package com.excel;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import com.excel.config.RsTableConfig;
import com.excel.config.TableConfigIFace;
import com.excel.exception.DBExcelException;
import com.excel.exception.ResultSetNullException;


public class ResultSetExcelWriter extends ExcelWriter { 
    /**
     * ResultSet
     */
    ResultSet resultSet;
    ResultSetMetaData rsmd;
    /**
     * 表头设置信息
     * 
     * @see MergedColumns
     */
    private String formatStr;

    public ResultSetExcelWriter(String fileName, ResultSet resultSet, String formatStr) {
	super(fileName);
	this.resultSet = resultSet;
	this.formatStr = formatStr;
    }

 
    public  void initConfig() {
	try {
	    rsmd = resultSet.getMetaData();
	} catch (SQLException e1) {	  
	    e1.printStackTrace();
	}
	// 取得配置信息
	TableConfigIFace rsTableConfig = RsTableConfig
		.getTableIndtance(resultSet);
	try {
	    table = rsTableConfig.getTable();
	} catch (DBExcelException e) {
	    e.printStackTrace();
	} catch (ResultSetNullException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	mergedColumns = new StringMergedColumns(formatStr);	
	mergedColumns.mergeColumn();
    }
    
   

    /**
     * 起始位置从(2,0)开始
     * 
     * @param resultSet
     */
    protected void writeDataSheet() {
	int rowIndex = this.tableYIndex + 2;
	int rows = 0;
	int colIndex = this.tableXIndex;
	dataRowCount = 0;
	ResultSet rs = getResultSet();
	MergedColumns mergedColumns = getMergedColumns();
	MyTable table = getTable();
	// 1.0取字段列表
	List<String> fieldNameList = mergedColumns.getAllFieldsList();
	int numColumns = fieldNameList.size();
	try {
	    while (rs.next()) {
	        for (int i = colIndex; i < numColumns + colIndex; i++) {
	    	String fieldName = fieldNameList.get(i - colIndex);
	    	// 取得列信息
	    	ColumnInfoBean columnInfoBean = table
	    		.getColumnInfoBean(fieldName);
	    	String type = columnInfoBean.getTypeName();
	    	int scale = Integer.parseInt(columnInfoBean.getScale());
	    	int presion = Integer.parseInt(columnInfoBean.getPrecision());
	    	// 整数
	    	if (type.equalsIgnoreCase("NUMBER") && presion == 0) {
	    	    WritableCellFormat cf2 = new WritableCellFormat(
	    		    NumberFormats.INTEGER);

	    	    cf2.setAlignment(Alignment.LEFT);

	    	    Number n = new Number(i, rowIndex, rs.getDouble(fieldName),
	    		    cf2);
	    	    s.addCell(n);
	    	}
	    	// 小数
	    	else if (type.equalsIgnoreCase("NUMBER") && presion > 0) {
	    	    WritableCellFormat cf2 = new WritableCellFormat(
	    		    NumberFormats.FLOAT);
	    	    Number n = new Number(i, rowIndex, rs.getDouble(fieldName),
	    		    cf2);
	    	    s.addCell(n);
	    	} else {
	    	    Label l = new Label(i, rowIndex, rs.getString(fieldName));
	    	    s.addCell(l);
	    	}

	        }
	        rowIndex++;
	        dataRowCount++;
	    }
	} catch (RowsExceededException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (NumberFormatException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (WriteException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (SQLException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }

    public MergedColumns getMergedColumns() {
	return mergedColumns;
    }

    public void setMergedColumns(MergedColumns mergedColumns) {
	this.mergedColumns = mergedColumns;
    }

    public ResultSet getResultSet() {
	return resultSet;
    }

   

  

  
}
